package cn.edu.heuet.bnve.fragment;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.xuexiang.xui.adapter.FragmentAdapter;
import com.xuexiang.xui.widget.tabbar.EasyIndicator;

import java.util.ArrayList;
import java.util.List;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.base.fragment.BaseFragment;
import cn.edu.heuet.bnve.databinding.FragmentTodoBinding;

public class TodoFragment extends BaseFragment {

    EasyIndicator mEasyIndicator;
    ViewPager mViewPager;

    private TodoViewModel mViewModel;

    private FragmentTodoBinding binding;
    List<Fragment> fragments;

    public static TodoFragment newInstance() {
        return new TodoFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_todo;
    }

    @Override
    protected void initView() {
        binding= DataBindingUtil.bind(this.mRootView);
        mEasyIndicator = binding.easyIndicator;
        mViewPager = binding.viewPager;
        fragments = new ArrayList<>();
        fragments.add(ScheduleFragment.newInstance());
        fragments.add(ScheduleListFragment.newInstance(0));
        fragments.add(ScheduleListFragment.newInstance(1));
        mEasyIndicator.setTabTitles(ContentPage.getPageNames());
        mEasyIndicator.setViewPager(mViewPager, new FragmentAdapter<>(getChildFragmentManager(), fragments));
        mViewPager.setOffscreenPageLimit(ContentPage.size() - 1);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override
            public void onPageSelected(int position) {
                fragments.get(position).onStart();
            }
            @Override
            public void onPageScrollStateChanged(int state) {}
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(TodoViewModel.class);
        // TODO: Use the ViewModel
    }

    @Override
    public void onStart() {
        super.onStart();
        if(fragments!=null) {
            for (Fragment fragment : fragments) {
                fragment.onStart();
            }
        }
    }
}